<template>
  <div>
    <el-button icon="el-icon-back" circle @click="back"></el-button>
    <el-form ref="form" :model="form" status-icon :rules="rules" label-width="80px">
      <el-form-item label="用户编号">
        <el-input v-model="form.uid" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="机构编号" prop="jid">
        <el-input v-model="form.jid"></el-input>
      </el-form-item>
      <el-form-item label="用户名称" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="用户密码" prop="password">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名" prop="truename">
        <el-input v-model="form.truename"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="idcardnumber">
        <el-input v-model="form.idcardnumber"></el-input>
      </el-form-item>
      <el-form-item label="用户权限" prop="authority">
        <el-select v-model="form.authority" placeholder="请选择权限类型">
          <el-option label="高" value="高"></el-option>
          <el-option label="中" value="中"></el-option>
          <el-option label="低" value="低"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户身份" prop="identity">
        <el-select v-model="form.identity" placeholder="请选择身份类型">
          <el-option label="消防人员" value="消防人员"></el-option>
          <el-option label="司法人员" value="司法人员"></el-option>
          <el-option label="技术服务人员" value="技术服务人员"></el-option>
          <el-option label="鉴定机构人员" value="鉴定机构人员"></el-option>
          <el-option label="系统管理员" value="系统管理员"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="upd">更新</el-button>
        <el-button @click="cancel">恢复</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "EditUser",
  uid:'',
  data(){
    return{
      temp:[],
      form: {
        uid:'',
        jid: '',
        username: '',
        password: '',
        truename: '',
        idcardnumber: '',
        authority: '',
        identity: ''
      },
      rules: {
        jid: [
          {required: true, message: '账号不可为空', trigger: 'blur'}
        ],
        username: [
          {required: true, message: '账号不可为空', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '账号不可为空', trigger: 'blur'}
        ],
        truename: [
          {required: true, message: '账号不可为空', trigger: 'blur'}
        ],
        idcardnumber: [
          {required: true, message: '账号不可为空', trigger: 'blur'}
        ],
        authority: [
          {required: true, message: '账号不可为空', trigger: 'blur'}
        ],
        identity: [
          {required: true, message: '账号不可为空', trigger: 'blur'}
        ]
      },
    }
  },
  created: function() {
    this.uid=this.$route.params.uid;
  },
  beforeRouteEnter: (to,from,next) => {
    console.log('进入路由之前');//加载数据
    next(vm => {
      vm.selectinfo();
    })
  },
  methods:{
    back: function (){
      this.$router.push("/func/userList");
    },
    selectinfo: function() {
      this.axios({
        method:"post",
        headers: { 'content-type': 'application/json' },
        url: "http://evidence.frogking.cn/userAdmController/selectByUid",
        params:{
          token:localStorage.getItem("token_only"),
          uid:this.uid
        }
      }).then(res=>{
        console.log(res)
        if(res["data"]["code"]==='206'){
          this.form=res["data"]["item"];
          this.temp=this.info
        }
        else if(res["data"]["code"]==='210'){
          this.$alert('没有用户', '错误提示', {
            confirmButtonText: '确定'
          });
        }
        else if(res["data"]["code"]==='000'){
          this.$alert('token失效', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push("/login");
              localStorage.removeItem("token_only");
            }
          });
        }
        else if(res["data"]["code"]==='001'){
          this.$alert('身份不满足，禁止操作', '错误提示', {
            confirmButtonText: '确定'
          });
        }
        else if(res["data"]["code"]==='002'){
          this.$alert('权限不满足，禁止操作', '错误提示', {
            confirmButtonText: '确定'
          });
        }
      }).catch(function (error) {
        console.log(error);
      });
    },
    upd:function(){
      this.axios({
        method:"post",
        headers: { 'content-type': 'application/json' },
        url: "http://evidence.frogking.cn/userAdmController/update",
        params:{
          token:localStorage.getItem("token_only"),
          uid: this.form.uid,
          jid: this.form.jid,
          username : this.form.username,
          password : this.form.password,
          truename : this.form.truename,
          idcardNumber : this.form.idcardnumber,
          authority : this.form.authority,
          identity : this.form.identity
        }
      }).then(res=>{
        console.log(res)
        if(res["data"]["code"]==='200'){
          this.$router.push('/func/userList')
        }
        else if(res["data"]["code"]==='201'){
          this.$alert('系统错误！更新异常！', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push("/func/userList");
            }
          });
        }
        else if(res["data"]["code"]==='213'){
          this.$alert('对象不可修改！更新异常！', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push("/func/userList");
            }
          });
        }
        else if(res["data"]["code"]==='212'){
          this.$alert('权限错误！更新异常！', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push("/func/userList");
            }
          });
        }
        else if(res["data"]["code"]==='211'){
          this.$alert('身份证号格式不匹配！更新异常！', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push("/func/userList");
            }
          });
        }
        else if(res["data"]["code"]==='209'){
          this.$alert('属性错误！更新异常！', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push("/func/userList");
            }
          });
        }
        else if(res["data"]["code"]==='000'){
          this.$alert('token失效', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push("/login");
              localStorage.removeItem("token_only");
            }
          });
        }
        else if(res["data"]["code"]==='001'){
          this.$alert('token权限不满足，禁止操作', '错误提示', {
            confirmButtonText: '确定'
          });
        }
        else if(res["data"]["code"]==='002'){
          this.$alert('token权限不满足，禁止操作', '错误提示', {
            confirmButtonText: '确定'
          });
        }
      }).catch(function (error) {
        console.log(error);
      });
    },
    cancel:function (){
        this.selectinfo();
    }
  }

}
</script>
<style scoped>

</style>
